//
//  ByLoginUserOperationView.m
//  beibei_ios
//
//  Created by mc on 16/4/26.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByLoginUserOperationView.h"
#import "ByRegisterVC.h"   //注册
#import "ByForgetPwdVC.h"  //忘记密码
#import "ByLoginViewModel.h"

@interface ByLoginUserOperationView()<LoginDelegate>
@property (nonatomic, assign)NSInteger line;               //行数
@property (nonatomic, strong)NSArray *imgAry;              //图片数组
@property (nonatomic, strong)NSArray *titlesAry;           //标题数组
@property (nonatomic, assign)BOOL isShow;                  //是否显示密码
@property (nonatomic, strong)UIImageView *rightImg;        //是否显示密码图片
@property (nonatomic, strong)ByLoginViewModel *viewmodel;

@end
@implementation ByLoginUserOperationView
- (instancetype)initWithFrame:(CGRect)frame Titles:(NSArray *)titleAry  ImageAry:(NSArray *)imgArr{
    if (self = [super initWithFrame:frame]) {
        _titlesAry = titleAry;
        _imgAry = imgArr;
        self.width = frame.size.width;
        self.height = frame.size.height;
        _isShow = NO;
        [self _initView];//创建视图
    }
    return self;
}

//创建视图
- (void)_initView{
    self.backgroundColor = [UIColor whiteColor];
    CGFloat x,y,width,height;
    //循环创建textfiled
    ByTextField *filed ;
    for (int i = 0; i<_titlesAry.count; i++) {
        CGFloat margin = 15;//文本输入框之间的间距
        x = 30;
        y = LineHeight *i + margin*i + 40;
        width = self.frame.size.width - x *2;
        filed = [[ByTextField alloc] initWithFrame:CGRectMake(x, y, width, LineHeight)];

        if ([_titlesAry[i] containsString:@"手机号"] || [_titlesAry[i] containsString:@"验证码"]) {
            filed.keyboardType = UIKeyboardTypeNumberPad;
        }
        if ([_titlesAry[i] containsString:@"密码"]) {
            filed.secureTextEntry = YES;
        }
        if (_imgAry.count != 0) {
            UIImageView *leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, LineHeight - 10, LineHeight - 10)];
            [leftImg setImage:[UIImage imageNamed:_imgAry[i]]];
            UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LineHeight + 10, LineHeight)];
            [leftView addSubview:leftImg];
            filed.leftView = leftView;
            filed.leftViewMode = UITextFieldViewModeAlways;
            
        }
        NSMutableAttributedString *paleceholderAttr = [[NSMutableAttributedString alloc] initWithString:_titlesAry[i]];
        [paleceholderAttr addAttribute:NSFontAttributeName value:FONT(16) range:NSMakeRange(0, paleceholderAttr.length)];
        [paleceholderAttr addAttribute:NSForegroundColorAttributeName value:HTB_TEXT_COLOR2 range:NSMakeRange(0, paleceholderAttr.length)];
        [filed setAttributedPlaceholder:paleceholderAttr];
        filed.tag = 10+ i;
        [self addSubview:filed];
        
        //创建线
        y = filed.bottom;
        width = SCREEN_WIDTH - 2*x;
        height = 1;
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        line.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
        [self addSubview:line];

        //第二栏添加图片
        if (i == 1) {
            _rightImg = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, LineHeight - 10, LineHeight - 10)];
            _rightImg.userInteractionEnabled = YES;
            _rightImg.image = [UIImage imageNamed:@"login_notSee"];
            filed.rightView = _rightImg;
            filed.rightViewMode = UITextFieldViewModeAlways;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
            [_rightImg addGestureRecognizer:tap];
        }
    }
    //登录按钮
    filed = (ByTextField *)[self viewWithTag:11];
    x = 40;
    y = filed.bottom + 30;
    height = 40;
    width = SCREEN_WIDTH - 2*x;
    UIButton *loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [loginBtn setTitle:@"登 录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.backgroundColor = HTB_THEME_COLOR;
    loginBtn.tag = 111;
    [self addSubview:loginBtn];
    [loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    
    //注册
    y = loginBtn.bottom + 15;
    UIButton *registBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [registBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registBtn setTitleColor:HTB_TEXT_COLOR2 forState:UIControlStateNormal];
    registBtn.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [self addSubview:registBtn];
    [registBtn addTarget:self action:@selector(registAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //忘记密码
    y = registBtn.bottom + 5;
    NSString *str = @"忘记密码";
    width = [str getSizeOfString:16].width;
    height = 40;
    x = SCREEN_WIDTH - 40 - width;
    UIButton *forgetBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [forgetBtn setTitleColor:[UIColor colorWithR:124 g:206 b:188] forState:UIControlStateNormal];
    [forgetBtn setTitle:str forState:UIControlStateNormal];
    forgetBtn.titleLabel.font = FONT(16);
    [self addSubview:forgetBtn];
    [forgetBtn  addTarget:self action:@selector(forgetAction:) forControlEvents:UIControlEventTouchUpInside];
    
 }
#pragma mark ---点击事件
//显示密码or隐藏
- (void)click:(UITapGestureRecognizer *) tap{
    ByTextField *filed = (ByTextField *)[self viewWithTag:11];
    _isShow = !_isShow;
    if (_isShow) {
        filed.secureTextEntry = NO;
        _rightImg.image = [UIImage imageNamed:@"login_see"];
    }else{
        filed.secureTextEntry = YES;
        _rightImg.image = [UIImage imageNamed:@"login_notSee"];
    }
}
//登录
- (void)login:(UIButton *)btn{
    ByTextField *phonefiled = (ByTextField *)[self viewWithTag:10];
    ByTextField *pwdfiled = (ByTextField *)[self viewWithTag:11];
    if ([phonefiled.text isStringNull]) {
        [ByCoreToolAlert showFailureWithText:@"手机号不能为空"];
        return;
    }
    if ([pwdfiled.text isStringNull]) {
        [ByCoreToolAlert showFailureWithText:@"密码不能为空"];
        return;
    }
    if (phonefiled.text.length != 11) {
        [ByCoreToolAlert showFailureWithText:@"手机号输入不正确"];
        return;
    }
    _viewmodel = [[ByLoginViewModel alloc] init];
    _viewmodel.loginDelegate = self;
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.username = phonefiled.text;
    model.password = pwdfiled.text;
    [_viewmodel login:model];
}
//忘记密码
//- (void)forgetPwd:(UITapGestureRecognizer *)tap{
//    ByForgetPwdVC *vc = [[ByForgetPwdVC alloc] init];
//    ByBaseVC *superVC = (ByBaseVC *)self.viewController;
//    [superVC by_normalPush:vc hasNavi:YES];
//}
/***忘记密码*/
- (void)forgetAction:(UIButton *)btn{
    ByForgetPwdVC *vc = [[ByForgetPwdVC alloc] init];
    ByBaseVC *superVC = (ByBaseVC *)self.viewController;
    [superVC by_normalPush:vc hasNavi:YES];

}

/***注册*/
- (void)registAction:(UIButton *)btn{
    ByRegisterVC *registerVC = [[ByRegisterVC alloc] init];
    ByBaseVC *superVC = (ByBaseVC *)self.viewController;
    [superVC by_normalPush:registerVC hasNavi:YES];
}
//手机注册
//- (void)phoneRegister:(UITapGestureRecognizer *)tap{
//    ByRegisterVC *registerVC = [[ByRegisterVC alloc] init];
//    ByBaseVC *superVC = (ByBaseVC *)self.viewController;
//    [superVC by_normalPush:registerVC hasNavi:YES];
//}
#pragma mark  -----相关代理
- (void)loginSuccess:(ByNetBaseResult *)result{
    NSLog(@"!!!!!!!!!!!!!!------%@",result.data);
    
//    ByNetUserModel *model = (ByNetUserModel *)result.data;
//    // 缓存用户model
//    [ByCommonCache setUserInfo:model];
//    // 友盟设置别名
//    NSString *alisa = [NSString stringWithFormat:@"%@", BY_USER.uid];
//    [UMessage setAlias:alisa type:@"ihome" response:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
//        if (error) {
//            NSString *str = error.domain;
//            printf("别名绑定_error:%s", [str UTF8String]);
//        } else {
//            printf("别名绑定成功");
//        }
//    }];
    // 登录☁️旺
//    [self IMLoginWithUserID:model.im_username password:model.im_password];
    
//    [self.viewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)loginFail:(ByNetBaseResult *)result{
    [ByCoreToolAlert showFailureWithText:result.msg];
}


//- (void)IMLoginWithUserID:(NSString *)userID password:(NSString *)password {
//    ZKIMKitHandle *handle = [ZKIMKitHandle sharedInstance];
//    
//    [handle callThisAfterISVAccountLoginSuccessWithYWLoginId:userID passWord:password preloginedBlock:^{
//        NSLog(@"预登录成功");
//        
//    } successBlock:^{
//
//        [ByCoreToolAlert showOnlyText:@"登录成功"];
//        
//    } failedBlock:^(NSError *error) {
//        [ByCoreToolAlert showOnlyText:@"登录失败!"];
//    }];
//
//}
@end
